package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Optional;

@Controller
@RequestMapping("sellerController")
public class SellerController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(SellerController.class);

    @Reference(check=false ,timeout=16666)
    private SellerService sellerService;

    //跳转到登录页面
    @RequestMapping("sellerLoginList")
    public String sellerLoginList(){
        return "shoplogin";
    }
    //跳转到注册页面
    @RequestMapping("registerList")
    public String register(){
        return "register";
    }
    //注册
    @RequestMapping("register")
    @ResponseBody
    public Result register(TbSeller seller){
        try {
            String sellerId = seller.getSellerId();
            TbSeller seller1 = sellerService.pdSellerId(sellerId);
            Optional<TbSeller> tbSeller = Optional.ofNullable(seller1);
            if (tbSeller.isPresent()){
                return Result.response(ResultEnum.SUCCESS).setData(1);
            }
            String salt = ShiroUtils.generateSalt(6);//获取盐，加密用的key
            String md5 = ShiroUtils.encryptPassword("MD5", seller.getPassword(), salt, 10);
            seller.setSalt(salt);
            seller.setPassword(md5);
            logger.info("注册方法开始运行{}");
            sellerService.register(seller);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }
    //登录
    @RequestMapping("sellerLogin")
    public String login(String sellerId, String password, Model model){
        //使用shiro编写认证操作
        //1、获取subject
        Subject subject = SecurityUtils.getSubject();
        //2、封装用户信息
        UsernamePasswordToken token = new UsernamePasswordToken(sellerId,password);
        //3、执行登录方法
        try {
            subject.login(token);
            //登录成功
            return "redirect:/sellerController/indexList";
        }catch (UnknownAccountException e){
            model.addAttribute("msg","用户不存在或者用户未认证");
            return "shoplogin";
        }catch (IncorrectCredentialsException e){
            //登录失败，密码错误
            model.addAttribute("msg","密码错误");
            return "shoplogin";
        }
    }
    //跳转到商家展示页面
    @RequestMapping("indexList")
    public String index(){
        return "admin/index";
    }

}
